import { defineConfig, loadEnv, UserConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import electron from 'vite-plugin-electron'
import renderer from 'vite-plugin-electron-renderer'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver, AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { resolve } from 'path'
import { viteMockServe } from 'vite-plugin-mock'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import alias from '@rollup/plugin-alias'
function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir)
}
// https://vitejs.dev/config/
export default defineConfig(({ mode }): UserConfig => {
  const { VITE_PORT } = loadEnv(mode, process.cwd())
  let port = +VITE_PORT
  return {
    plugins: [
      vue(),
      electron([
        {
          entry: 'src/background.ts',
          onstart(options) {
            options.startup()
          },
          vite: {
            build: {
              rollupOptions: {
                // 在 Rollup 的构建过程中，它会将所有依赖项打包到最终输出中。然而，在某些场景下，我们并不希望某些模块或依赖项被打包。此时，external 属性就派上用场了
                external: ['serialport', 'sqlite3'],
                // external: Object.keys(pkg.dependencies),
                plugins: [
                  // 主进程中使用路径别名
                  alias({
                    entries: [
                      // /@/xxxx => src/xxxx
                      {
                        find: /\/@\//,
                        replacement: pathResolve('src') + '/'
                      },
                      // /#/xxxx => types/xxxx
                      {
                        find: /\/#\//,
                        replacement: pathResolve('types') + '/'
                      }
                    ]
                  })
                ]
              },
              outDir: './dist'
            }
          }
        },
        {
          entry: 'src/utils/electron/preload.ts',
          vite: {
            build: {
              rollupOptions: {
                plugins: [
                  // 主进程中使用路径别名
                  alias({
                    entries: [
                      // /@/xxxx => src/xxxx
                      {
                        find: /\/@\//,
                        replacement: pathResolve('src') + '/'
                      },
                      // /#/xxxx => types/xxxx
                      {
                        find: /\/#\//,
                        replacement: pathResolve('types') + '/'
                      }
                    ]
                  })
                ]
              },
              outDir: './dist'
            }
          }
        }
      ]),
      AutoImport({
        dts: 'types/auto-imports.d.ts',
        imports: ['vue', 'vue-router', 'pinia', 'vue-i18n'],
        resolvers: [
          ElementPlusResolver(),
          AntDesignVueResolver(),
          IconsResolver({
            prefix: 'Icon'
          })
        ]
      }),
      Components({
        dts: 'types/components.d.ts',
        resolvers: [
          ElementPlusResolver(),
          AntDesignVueResolver({
            importStyle: false
          }),
          IconsResolver({
            enabledCollections: ['ep']
          })
        ]
      }),
      Icons({
        autoInstall: true
      }),
      viteMockServe({
        logger: true, //是否在控制台显示请求日志
        mockPath: './src/mock'
      }),
      // 注册所有的svg文件生成svg雪碧图
      createSvgIconsPlugin({
        // svg 图标存放的文件夹路径
        iconDirs: [pathResolve('src/assets/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
        // 插入的位置
        inject: 'body-last',
        // svg 的图标的id
        customDomId: '__svg__icons__dom__'
      }),
      // 在渲染进程中使用 electron 相关 api
      renderer({
        resolve: {
          serialport: { type: 'cjs' },
          sqlite3: { type: 'cjs' },
          'electron-store': { type: 'cjs' },
          'fs-extra': { type: 'esm' },
          '@prisma/client': { type: 'esm' }
        }
      })
    ],
    server: {
      host: '0.0.0.0',
      port: port || 4399
    },
    resolve: {
      // 配置路径别名
      alias: [
        // /@/xxxx => src/xxxx
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/'
        },
        // /#/xxxx => types/xxxx
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/'
        }
      ],
      // 引入文件的时候，可以忽略掉以下文件后缀
      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    base: './'
  }
})
